﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TaxAssistantBase;

namespace ShiBieProject
{
    class InvoiceMoneysCheck
    {
        //1.1赛能得到扫描后的金额，税率，税额，价税合计啊，大写的价税合计
        //1.2第一个判断，金额+税额是否等于价税合计，价税合计是否等于大写 的价税合计，如果都成立
        //1.3在判断是否为销项票，如果是销项票税率（或者商品名称）是否存在，如存在则用该税率进行计算如果金额*税率=原始的税额，1.2的判断也通过那么证明票据金额等是正确的税率正确
        //2.1如果金额是由二维码生成则一定正确，这是如果金额+税额=价税合计同时价税合计=大写价税合计证明价税合计，税额正确，从而可以计算出税率
        //2.2如果金额+税额不等于价税合计，价税合计=大写价税合计，证明，税额错误，价税合计正确，从而可计算出税额，在通过（若果是销项或者商品名称可以查出税率）金额*税率与价税合计-金额一致则证明计算得到的税额一定正确，在用金额+税额看是否等于价税合计，来判断是否完全正确
        //3.3，如果金额由二维码生成，有事销项票还有税率，那么金额*税率（不一定正确）=税额同时金额+得到的税额要等于价税合计，价税合计要等于大写价税合计，（如果价税合计不等于大写，还需在审核）
        //4.如果金额二维码没有得出，1 都未能得出那么要进行深一层次的校验
        private static string[] taxRateList = { "0%", "3%", "4%", "5%", "6%", "9%", "10%", "11%", "13%", "16%", "17%", "18%" };
        public static void moneyChuLi(List<InvoiiceOcrModel> bpmlist)
        {
            int exerro = 0;

            foreach (InvoiiceOcrModel bpm in bpmlist)
            {
                try
                {
                    if (bpm.companyState == "1") {
                        continue;
                    }
                    //if (bpm.billtype!=-1 && (bpm.invoiceOcrSonListSet[0].readstate == "4" || bpm.invoiceOcrSonListSet[0].readstate == "3" || bpm.invoiceOcrSonListSet[0].readstate == "2"))
                    //bpm.invoiceOcrSonLists.Count>0
                    if (bpm.billtype != -1 && bpm.invoiceOcrSonLists != null)
                    {
                        if (bpm.invoiceOcrSonLists.Count == 1)
                        {
                            if ((!string.IsNullOrEmpty(bpm.invoiceOcrSonLists[0].moneyAmounts) || !string.IsNullOrEmpty(bpm.invoiceOcrSonLists[0].taxAmounts) || !string.IsNullOrEmpty(bpm.invoiceOcrSonLists[0].totalMoney)))
                            {
                                exerro++;
                                string companyName = bpm.companyName;//公司名称
                                int id = bpm.id;//公司名称
                                string money = bpm.invoiceOcrSonLists[0].moneyAmounts;//金额
                                string taxAmounts = bpm.invoiceOcrSonLists[0].taxAmounts;//税额
                                string taxRate = bpm.invoiceOcrSonLists[0].taxRate;//税率
                                string totalMoney = bpm.invoiceOcrSonLists[0].totalMoney;//价税合计
                                string moneytaxhjMax = bpm.invoiceOcrSonLists[0].moneytaxhjMax;//价税合计大写
                                string isProviderOrCustomer = bpm.invoiceOcrSonLists[0].isProviderOrCustomer;//进销项标志
                                string invoiceMonyeState = bpm.invoiceOcrSonLists[0].moneyAmountsState;//金额获取途径标志
                                string qrCode = bpm.invoiceOcrSonLists[0].qrCode;//二维码
                                string productName = "";//如果只有一条或者商品名称一致那么可适用此值
                                string spareTaxRate = "";//如果是销项票才可以用此值
                                string productTaxRate = "";//如果是商品名称才可以用此值
                                productName = getProductName(bpm);// 获取商品名称
                                money = changNumberDelZero(money);
                                taxAmounts = changNumberDelZero(taxAmounts);
                                totalMoney = changNumberDelZero(totalMoney);
                                bpm.invoiceOcrSonLists[0].moneyAmounts = Convert.ToString(money);//金额
                                bpm.invoiceOcrSonLists[0].taxAmounts = Convert.ToString(taxAmounts);//税额
                                bpm.invoiceOcrSonLists[0].totalMoney = Convert.ToString(totalMoney);//价税合计
                                //  HongFengCompanyOrVendorsTool.ProductNameAll;
                                //   HongFengCompanyOrVendorsTool.companyAll;
                                if (productName != "")
                                {
                                    productTaxRate = getProductTaxRate(productName);
                                }
                                decimal moneyAmount;
                                decimal taxAmount;
                                decimal totleAmount;
                                decimal.TryParse(money, out moneyAmount);
                                decimal.TryParse(taxAmounts, out taxAmount);
                                decimal.TryParse(totalMoney, out totleAmount);
                                //判断价税合计和大写价税合计是否一致，一致（true）不一致（false）
                                bool isIdentical = minMoneyEquelMaxMoney(moneytaxhjMax, totleAmount);                        
                                #region 判断税率是否等于免税或不征税等字样
                                if (!string.IsNullOrEmpty(taxRate))
                                {
                                    if (getTaxRate(taxRate))
                                    {

                                        decimal moneysTempTest = Math.Round((moneyAmount + 0), 2, MidpointRounding.ToEven);
                                        bool isIdenticalMAndT = minMoneyEquelMaxMoney(moneytaxhjMax, moneysTempTest);
                                        if (bpm.invoiceOcrDetailedDtoList.Count > 1)
                                        {
                                            bool checksuccorfail = checkTaxRateisEquale(bpm.invoiceOcrDetailedDtoList);
                                            if (checksuccorfail)
                                            {
                                                taxRate = "0%";
                                                bpm.invoiceOcrSonLists[0].taxRate = "0%";
                                                if (!isIdenticalMAndT)
                                                {
                                                    bpm.invoiceOcrSonLists[0].taxAmounts = "0";
                                                    taxAmounts = "0";
                                                }
                                            }
                                            else
                                            {
                                                bpm.invoiceOcrSonLists[0].taxRate = "";
                                                taxRate = "";
                                            }
                                        }
                                        else
                                        {
                                            bpm.invoiceOcrSonLists[0].taxRate = "0%";
                                            if (!isIdenticalMAndT)
                                            {
                                                bpm.invoiceOcrSonLists[0].taxAmounts = "0";
                                                taxAmounts = "0";
                                            }
                                        }

                                    }
                                    else if (taxRate.Contains("*"))
                                    {
                                        taxRate = "空";
                                    }
                                }
                                #endregion
                                string gettaxRate = getTaxRate(spareTaxRate, productTaxRate, taxRate);
                                //金额有缺失
                                moneysIsNullEvery(bpm, money, taxAmounts, totalMoney, moneytaxhjMax, gettaxRate, isIdentical, ref moneyAmount, ref taxAmount, ref totleAmount);
                                if (isProviderOrCustomer == "0")
                                {
                                    spareTaxRate = getSpareTaxRate(companyName);
                                }
                                //    if (!string.IsNullOrEmpty(money)&&!string.IsNullOrEmpty(totalMoney)) {
                                if (invoiceMonyeState == CompanyOrMoneyStateEnum.InvoiceMoneyHaveQRcodeInfo || !string.IsNullOrEmpty(qrCode))
                                {
                                    #region 金额完全正确由二维码获取
                                    decimal moneys = Math.Round((moneyAmount + taxAmount), 2, MidpointRounding.ToEven);
                                    bool ifequals = minMoneyEquelMaxMoney(moneytaxhjMax, moneys);
                                    if (moneys == totleAmount)
                                    {
                                        if (isIdentical || ifequals)
                                        {
                                            setTaxRate(moneyAmount, taxAmount, bpm);
                                        }
                                        else
                                        {
                                            //通过金额（一定正确） 和如果得到销项公司中的税率或者商品名称对应的税率则可以计算的出税额从而在算出价税合计在与大小写比对
                                            //金额（一定正确）+错误的税额等于扫描后错误的价税合计，基本可以肯定税额和价税合计是对的
                                            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxAmount);
                                            if (resaulttaxlv != "" && resaulttaxlv != null)
                                            {
                                                if (resaulttaxlv != taxRate)
                                                {
                                                    getTaxAmountAndTotalAmount(resaulttaxlv, moneyAmount, taxAmount, totleAmount, moneytaxhjMax, bpm);
                                                    bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                }
                                                else
                                                {
                                                    getTaxAmountAndTotalAmount(resaulttaxlv, moneyAmount, taxAmount, totleAmount, moneytaxhjMax, bpm);
                                                    bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                }
                                            }
                                            else
                                            {
                                                string taxlvs = getTaxRate(spareTaxRate, productTaxRate, taxRate);
                                                decimal temptaxrate;
                                                decimal.TryParse(taxlvs.Replace("%", ""), out temptaxrate);
                                                if (!string.IsNullOrEmpty(taxlvs))
                                                {
                                                    decimal taxamount = Math.Round((moneyAmount * (temptaxrate / 100)), 2, MidpointRounding.ToEven);
                                                    decimal totleMoney = moneyAmount + taxamount;
                                                    string omparisonTaxmoney = getTaxAmounts(taxamount, taxAmount);
                                                    if (string.IsNullOrEmpty(omparisonTaxmoney))
                                                    {
                                                        bpm.invoiceOcrSonLists[0].taxAmounts = taxamount.ToString("#0.00");
                                                        bpm.invoiceOcrSonLists[0].totalMoney = totleMoney.ToString("#0.00");
                                                    }
                                                    bpm.invoiceOcrSonLists[0].taxRate = taxlvs;
                                                }
                                                else
                                                {
                                                    bpm.invoiceOcrSonLists[0].taxRate = taxlvs;
                                                }
                                            }
                                        }
                                    }
                                    else if (moneys != totleAmount)
                                    {
                                        if (isIdentical)
                                        {
                                            if (moneys > 0)
                                            {
                                                if (totleAmount < 0)
                                                {
                                                    totleAmount = Math.Abs(totleAmount);
                                                    bpm.invoiceOcrSonLists[0].totalMoney = totleAmount.ToString("#0.00");
                                                }
                                            }
                                            //因为金额由二维码得出一定正确，大写又与价税合计相等，则可以通过价税合计-金额得出税额，从而得出税率
                                            decimal taxmoney = Math.Round((totleAmount - moneyAmount), 2, MidpointRounding.ToEven);
                                            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxmoney);
                                            bpm.invoiceOcrSonLists[0].taxAmounts = taxmoney.ToString("#0.00");
                                            bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                        }
                                        else
                                        {
                                            //通过金额和得到的税率来得出最终的价税合计和税额，在与价税合计大写比较
                                            bool isequals = minMoneyEquelMaxMoney(moneytaxhjMax, moneys);
                                            //如果计算得出的价税合计转大写后与扫描得出大写一致则证明，税额基本正确
                                            if (isequals)
                                            {
                                                string resaulttaxlv = calculatingTaxRate(moneyAmount, taxAmount);
                                                if (resaulttaxlv != taxRate)
                                                {
                                                    bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                }
                                                bpm.invoiceOcrSonLists[0].totalMoney = moneys.ToString("#0.00");
                                                bpm.invoiceOcrSonLists[0].taxAmounts = taxAmount.ToString("#0.00");
                                            }
                                            else
                                            {
                                                if (taxAmount == 0 && taxRate == "0%")
                                                {
                                                    bpm.invoiceOcrSonLists[0].taxRate = "0%";
                                                    bpm.invoiceOcrSonLists[0].taxAmounts = "0";
                                                    bpm.invoiceOcrSonLists[0].totalMoney = money;
                                                }
                                                else
                                                {
                                                    decimal temptaxmoney = Math.Round((totleAmount - moneyAmount), 2, MidpointRounding.ToEven);
                                                    //通过临时税额得出税率
                                                    string resaulttaxlv = calculatingTaxRate(moneyAmount, temptaxmoney);
                                                    //通过扫描及是否销项或是否商品名称得出税率
                                                    if (!string.IsNullOrEmpty(resaulttaxlv))
                                                    {
                                                        if (resaulttaxlv != spareTaxRate)
                                                        {
                                                            spareTaxRate = "";
                                                        }
                                                    }
                                                    string temptaxlv = (getTaxRate(spareTaxRate, productTaxRate, taxRate)).Replace("%", "");
                                                    decimal temptaxrate;
                                                    if (!string.IsNullOrEmpty(temptaxlv))
                                                    {
                                                        decimal.TryParse(temptaxlv, out temptaxrate);
                                                    }
                                                    else
                                                    {
                                                        string restaxlv = "";
                                                        if (!string.IsNullOrEmpty(resaulttaxlv))
                                                        {
                                                            restaxlv = resaulttaxlv.Replace("%", "");
                                                        }
                                                        decimal.TryParse(restaxlv, out temptaxrate);
                                                    }
                                                    decimal taxmoney = Math.Round((moneyAmount * temptaxrate) / 100, 2, MidpointRounding.ToEven);
                                                    string endtaxAmount = getTaxAmounts(taxmoney, taxAmount);
                                                    string taxRatesfax = "";
                                                    if (!string.IsNullOrEmpty(temptaxlv))
                                                    {
                                                        taxRatesfax = temptaxlv + "%";
                                                    }
                                                    //if (endtaxAmount!=0) 证明得到的税率正确，得到的税额正确
                                                    if (!string.IsNullOrEmpty(temptaxlv))
                                                    {
                                                        if (!string.IsNullOrEmpty(endtaxAmount))
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = taxRatesfax;
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = taxAmount.ToString("#0.00");
                                                            decimal endtotalMney = Math.Round((moneyAmount + taxAmount), 2, MidpointRounding.ToEven);
                                                            bpm.invoiceOcrSonLists[0].totalMoney = endtotalMney.ToString("#0.00");
                                                        }
                                                        else
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = taxRatesfax;
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = taxmoney.ToString("#0.00");
                                                            decimal endtotalMney = Math.Round((moneyAmount + taxmoney), 2, MidpointRounding.ToEven);
                                                            bpm.invoiceOcrSonLists[0].totalMoney = endtotalMney.ToString("#0.00");
                                                        }
                                                    }
                                                    else
                                                    {
                                                        bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                        bpm.invoiceOcrSonLists[0].taxAmounts = taxmoney.ToString("#0.00");
                                                        decimal endtotalMoney = Math.Round((moneyAmount + taxmoney), 2, MidpointRounding.ToEven);
                                                        bpm.invoiceOcrSonLists[0].totalMoney = endtotalMoney.ToString("#0.00");
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (bpm.invoiceOcrDetailedDtoList != null)
                                    {
                                        if (bpm.invoiceOcrDetailedDtoList.Count > 0)
                                        {
                                            //1、总金额是由二维码提取而出必定正确，所以如果明细金额不等于金额，那么一定错误
                                            //2.1如果是进项票并且是普票（专票除外），不管有多少条都合并成一条并进行金额赋值明细金额要等于总金额
                                            //2.2如果是销项票不论是普票还是专票，都需要金额与总金额一致，暂时不需要汇总成一项
                                            if (isProviderOrCustomer == "1")
                                            {
                                                if (bpm.invoiceOcrDetailedDtoList.Count == 1)
                                                {
                                                    bpm.invoiceOcrDetailedDtoList[0].moneyAmount = bpm.invoiceOcrSonLists[0].moneyAmounts;
                                                    bpm.invoiceOcrDetailedDtoList[0].taxAmount = bpm.invoiceOcrSonLists[0].taxAmounts;
                                                    bpm.invoiceOcrDetailedDtoList[0].detaileTaxRate = bpm.invoiceOcrSonLists[0].taxRate;
                                                }
                                                else if (bpm.invoiceOcrDetailedDtoList.Count > 1)
                                                {
                                                    List<invoiceOcrDetailedModel> iodlist = bpm.invoiceOcrDetailedDtoList;
                                                    string initTaxRate = iodlist[0].detaileTaxRate;
                                                    bool ifbreak = false;
                                                    foreach (invoiceOcrDetailedModel iod in iodlist)
                                                    {
                                                        string detaileTaxRate = iod.detaileTaxRate;
                                                        if (initTaxRate != detaileTaxRate)
                                                        {
                                                            ifbreak = true;
                                                            break;
                                                        }
                                                    }
                                                    if (!ifbreak)
                                                    {//说明明细不等，不可合并
                                                        bpm.invoiceOcrDetailedDtoList = new List<invoiceOcrDetailedModel>();
                                                        iodlist[0].moneyAmount = bpm.invoiceOcrSonLists[0].moneyAmounts;
                                                        iodlist[0].taxAmount = bpm.invoiceOcrSonLists[0].taxAmounts;
                                                        iodlist[0].detaileTaxRate = bpm.invoiceOcrSonLists[0].taxRate;
                                                        bpm.invoiceOcrDetailedDtoList.Add(iodlist[0]);
                                                    }
                                                }
                                            }
                                            else if (isProviderOrCustomer == "0")
                                            {
                                                decimal detaileMoneyTotles;
                                                decimal detaileTaxMoneyTotles;
                                                decimal.TryParse(bpm.invoiceOcrSonLists[0].moneyAmounts, out detaileMoneyTotles);
                                                decimal.TryParse(bpm.invoiceOcrSonLists[0].taxAmounts, out detaileTaxMoneyTotles);
                                                invoiceDetailVserificationMoneyEtc.detailVserification(bpm.invoiceOcrDetailedDtoList, detaileMoneyTotles, detaileTaxMoneyTotles);
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    # region 金额由扫描识别获取，不保证一定正确
                                    //1.1判断初始值是否平衡
                                    decimal moneys = Math.Round((moneyAmount + taxAmount), 2, MidpointRounding.ToEven);
                                    decimal moneysAbs = Math.Abs(moneyAmount) + Math.Abs(taxAmount);
                                    bool ifequals = minMoneyEquelMaxMoney(moneytaxhjMax, moneys);
                                    if (moneys == totleAmount)
                                    {
                                        if (isIdentical || ifequals)
                                        {
                                            //得到税率  
                                            setTaxRate(moneyAmount, taxAmount, bpm);
                                        }
                                        else
                                        {
                                            //当金额+税额=价税合计，但是价税合计不等于大写价税合计是有两种情况
                                            //A 价税合计大写错误，这就好办，证明金额和税额都是对的
                                            //B 恰好金额税额价税合计处于都少识别了小数点或0，导致正确，但是这种情况很少，但也不能忽略这种，
                                            //但是大部分我都可认为是价税合计大写错误，特殊的情况暂时需手动修改
                                            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxAmount);
                                            if (!string.IsNullOrEmpty(resaulttaxlv))
                                            {
                                                if (resaulttaxlv != spareTaxRate)
                                                {
                                                    spareTaxRate = "";
                                                }
                                            }
                                            string tempgettaxlv = getTaxRate(spareTaxRate, productTaxRate, taxRate);
                                            if (!string.IsNullOrEmpty(resaulttaxlv))
                                            {
                                                if (resaulttaxlv != taxRate)
                                                {
                                                    decimal tempTaxMoney;
                                                    string getTaxAmount = getTaxAmounts(resaulttaxlv, moneyAmount, taxAmount, out tempTaxMoney);
                                                    if (resaulttaxlv == tempgettaxlv)
                                                    {
                                                        if (string.IsNullOrEmpty(getTaxAmount))
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = tempTaxMoney.ToString("#0.00");
                                                            bpm.invoiceOcrSonLists[0].taxRate = tempgettaxlv;
                                                            bpm.invoiceOcrSonLists[0].totalMoney = (tempTaxMoney + moneyAmount).ToString("#0.00");
                                                        }
                                                        else
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = getTaxAmount;
                                                            bpm.invoiceOcrSonLists[0].taxRate = tempgettaxlv;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (!string.IsNullOrEmpty(resaulttaxlv))
                                                        {
                                                            decimal tempFaxTaxMoney;
                                                            string getFaxTaxAmount = getTaxAmounts(resaulttaxlv, moneyAmount, taxAmount, out tempFaxTaxMoney);
                                                            if (string.IsNullOrEmpty(getFaxTaxAmount))
                                                            {
                                                                bpm.invoiceOcrSonLists[0].taxAmounts = tempFaxTaxMoney.ToString("#0.00");
                                                                bpm.invoiceOcrSonLists[0].totalMoney = (tempFaxTaxMoney + moneyAmount).ToString("#0.00");
                                                            }
                                                            else
                                                            {
                                                                bpm.invoiceOcrSonLists[0].totalMoney = (taxAmount + moneyAmount).ToString("#0.00");
                                                            }
                                                            bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;

                                                        }
                                                        else
                                                        {
                                                            if (string.IsNullOrEmpty(getTaxAmount))
                                                            {
                                                                bpm.invoiceOcrSonLists[0].taxAmounts = tempTaxMoney.ToString("#0.00");
                                                            }
                                                            bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                            bpm.invoiceOcrSonLists[0].totalMoney = (tempTaxMoney + moneyAmount).ToString("#0.00");
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                                }
                                            }
                                            else
                                            {
                                                bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
                                            }
                                        }

                                    }
                                    else if (moneys != totleAmount)
                                    {
                                        if (isIdentical)
                                        {
                                            //证明价税合计是正确的。可以通过税率，来计算得出金额从而得出税额 公式：价税合计/（1+税率）= 金额
                                            string temptaxlv = (getTaxRate(spareTaxRate, productTaxRate, taxRate)).Replace("%", "");
                                            decimal temptaxrate;
                                            decimal.TryParse(temptaxlv, out temptaxrate);
                                            //金额 *（1+税率）
                                            decimal moneyTotleCh = Math.Round((moneyAmount * (1 + (temptaxrate / 100))), 2, MidpointRounding.ToEven);
                                            decimal moneyAmoutFax;
                                            if (moneyTotleCh != totleAmount)
                                            {
                                                moneyAmoutFax = Math.Round((totleAmount / (1 + (temptaxrate / 100))), 2, MidpointRounding.ToEven);
                                                decimal ismoney = getmoneyEqual(moneyAmoutFax, moneyAmount);
                                                decimal taxmoney;
                                                string bigTaxRate = "";
                                                if (!string.IsNullOrEmpty(temptaxlv))
                                                {
                                                    bigTaxRate = temptaxlv + "%";
                                                    if (ismoney != 0)
                                                    {
                                                        taxMoneyAndTaxRate(totleAmount, moneyAmount, bpm);
                                                    }
                                                    else
                                                    {
                                                        taxMoneyAndTaxRate(totleAmount, moneyAmoutFax, bpm);
                                                    }
                                                }
                                                else
                                                {
                                                    //需核验
                                                    taxMoneyAndTaxRate(totleAmount, moneyAmount, bpm);
                                                }
                                            }
                                            else
                                            {
                                                string taxRateEndResault = "";
                                                decimal taxAmountab = moneyTotleCh - moneyAmount;
                                                bpm.invoiceOcrSonLists[0].taxAmounts = Convert.ToString(taxAmountab);
                                                if (!string.IsNullOrEmpty(temptaxlv))
                                                {
                                                    taxRateEndResault = temptaxlv + "%";
                                                }
                                                else
                                                {
                                                    taxRateEndResault = calculatingTaxRate(moneyAmount, taxAmountab);
                                                }
                                                bpm.invoiceOcrSonLists[0].taxRate = taxRateEndResault;
                                            }
                                        }
                                        else
                                        {
                                            //当都不想等的时候，是无法证明谁是正确的，但是大多是正确的是价税合计和大写价税合计，当他俩不相等时，大写价税合计更接近于真实值
                                            //利用大写价税合计和税率得出金额，从而得出税额，但肯定是要审核的
                                            string resaulttaxlv = (calculatingTaxRate(moneyAmount, taxAmount)).Replace("%", "");
                                            if (!string.IsNullOrEmpty(resaulttaxlv))
                                            {
                                                spareTaxRate = "";
                                            }
                                            string temptaxlv = (getTaxRate(spareTaxRate, productTaxRate, taxRate)).Replace("%", "");
                                            decimal temptaxrate;
                                            decimal tempresaulttaxlv;
                                            string endTaxRate = "";
                                            if (!string.IsNullOrEmpty(temptaxlv))
                                            {
                                                endTaxRate = temptaxlv + "%";
                                            }
                                            decimal.TryParse(temptaxlv, out temptaxrate);
                                            decimal.TryParse(resaulttaxlv, out tempresaulttaxlv);
                                            bool isequals = minMoneyEquelMaxMoney(moneytaxhjMax, moneys);
                                            //如果税率都存在
                                            if (resaulttaxlv == taxRate)
                                            {
                                                bpm.invoiceOcrSonLists[0].totalMoney = moneys.ToString("#0.00");
                                            }
                                            else if (temptaxlv == resaulttaxlv)
                                            {
                                                if (isequals)
                                                {
                                                    //if通过销项或者商品名称或者本身通过筛选的出的税率与金额和税额相除得到的税率一直那么暂时证明金额税额正确
                                                    bpm.invoiceOcrSonLists[0].taxRate = endTaxRate;
                                                    bpm.invoiceOcrSonLists[0].totalMoney = moneys.ToString("#0.00");
                                                }
                                                else
                                                {
                                                    //if通过销项或者商品名称或者本身通过筛选的出的税率与金额和税额相除得到的税率一直那么暂时证明金额税额正确
                                                    setInvoiceTaxRateAndTotleMoneyValue(bpm, tempresaulttaxlv, moneys, resaulttaxlv);
                                                }
                                            }
                                            else
                                            {
                                                if (isequals)
                                                {
                                                    setInvoiceTaxRateAndTotleMoneyValue(bpm, tempresaulttaxlv, moneys, resaulttaxlv);
                                                }
                                                else
                                                {
                                                    //金额*税率是否=税额（有两个税率都试一试）temptaxlv/resaulttaxlv
                                                    decimal taxAmountOne = 0;
                                                    decimal taxAmountTwo = 0;
                                                    decimal totleAmountOne = 0;
                                                    decimal totleAmountTwo = 0;
                                                    if (!string.IsNullOrEmpty(resaulttaxlv))
                                                    {
                                                        taxAmountOne = Math.Round((moneyAmount * tempresaulttaxlv), 2, MidpointRounding.ToEven);
                                                        totleAmountOne = Math.Round((moneyAmount + taxAmountOne), 2, MidpointRounding.ToEven);
                                                    }
                                                    if (!string.IsNullOrEmpty(temptaxlv))
                                                    {
                                                        taxAmountTwo = Math.Round((moneyAmount * temptaxrate), 2, MidpointRounding.ToEven);
                                                        totleAmountTwo = Math.Round((moneyAmount + taxAmountTwo), 2, MidpointRounding.ToEven);
                                                    }
                                                    bool isequalsOne = minMoneyEquelMaxMoney(moneytaxhjMax, totleAmountOne);
                                                    bool isequalsTwo = minMoneyEquelMaxMoney(moneytaxhjMax, totleAmountTwo);
                                                    string getTaxAmountOne = getTaxAmounts(taxAmountOne, taxAmount);
                                                    string getTaxAmountTwo = getTaxAmounts(taxAmountTwo, taxAmount);
                                                    if (!string.IsNullOrEmpty(getTaxAmountOne))
                                                    {
                                                        setInvoiceTaxRateAndTotleMoneyValue(bpm, tempresaulttaxlv, moneys, resaulttaxlv);
                                                    }
                                                    else if (!string.IsNullOrEmpty(getTaxAmountTwo))
                                                    {
                                                        setInvoiceTaxRateAndTotleMoneyValue(bpm, temptaxrate, moneys, temptaxlv);
                                                    }
                                                    else if (isequalsOne)
                                                    {
                                                        setInvoiceTaxRateAndTotleMoneyValue(bpm, tempresaulttaxlv, moneys, resaulttaxlv);
                                                    }
                                                    else if (isequalsTwo)
                                                    {
                                                        setInvoiceTaxRateAndTotleMoneyValue(bpm, temptaxrate, moneys, temptaxlv);
                                                    }
                                                    else if (totleAmountOne == totleAmount)
                                                    {
                                                        bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountOne.ToString("#0.00");
                                                        if (!string.IsNullOrEmpty(resaulttaxlv))
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = tempresaulttaxlv + "%";
                                                        }
                                                        else
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = "";
                                                        }

                                                    }
                                                    else if (totleAmountTwo == totleAmount)
                                                    {
                                                        bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountTwo.ToString("#0.00");
                                                        if (!string.IsNullOrEmpty(temptaxlv))
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = temptaxrate + "%";
                                                        }
                                                        else
                                                        {
                                                            bpm.invoiceOcrSonLists[0].taxRate = "";
                                                        }
                                                    }
                                                    else
                                                    {

                                                        decimal moneyAmoutFax;
                                                        //暂时认为价税合计正确
                                                        if (!string.IsNullOrEmpty(temptaxlv))
                                                        {
                                                            moneyAmoutFax = Math.Round((totleAmount / (1 + (temptaxrate / 100))), 2, MidpointRounding.ToEven);
                                                        }
                                                        else if (!string.IsNullOrEmpty(resaulttaxlv))
                                                        {
                                                            moneyAmoutFax = Math.Round((totleAmount / (1 + (tempresaulttaxlv / 100))), 2, MidpointRounding.ToEven);
                                                        }
                                                        else
                                                        {
                                                            moneyAmoutFax = moneyAmount;
                                                        }
                                                        decimal moneyAmoutEnd = getmoneyEqual(moneyAmoutFax, moneyAmount);
                                                        decimal taxAmountEnd = 0;
                                                        if (moneyAmoutEnd != 0)
                                                        {
                                                            taxAmountEnd = totleAmount - moneyAmount;
                                                            string taxlv = calculatingTaxRate(moneyAmount, taxAmountEnd);
                                                            bpm.invoiceOcrSonLists[0].taxRate = taxlv;
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountEnd.ToString("#0.00");
                                                        }
                                                        else
                                                        {
                                                            taxAmountEnd = totleAmount - moneyAmoutFax;
                                                            string raxRateCalTend = calculatingTaxRate(moneyAmoutFax, taxAmountEnd);
                                                            bpm.invoiceOcrSonLists[0].taxRate = raxRateCalTend;
                                                            bpm.invoiceOcrSonLists[0].moneyAmounts = moneyAmoutFax.ToString("#0.00");
                                                            bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountEnd.ToString("#0.00");
                                                        }
                                                    }
                                                }
                                            }

                                        }
                                    }
                                    #endregion
                                }
                            }
                        }

                    }

                }
                catch (Exception ex)
                {
                    WsdLogger.yxlog("error", "第" + exerro + "条验证错误" + ex.Message + "发票号为：" + bpm.invoiceNum + "批次号为：" + bpm.batchtime);
                    continue;
                }
            }



        }
        public static bool checkTaxRateisEquale(List<invoiceOcrDetailedModel> iodetailedList)
        {
            string detaiTaxRate = "";
            foreach (invoiceOcrDetailedModel idm in iodetailedList)
            {
                string detailedTaxRate = idm.detaileTaxRate;
                if (string.IsNullOrEmpty(detaiTaxRate))
                {
                    detaiTaxRate = detailedTaxRate;
                }
                else
                {
                    if (detaiTaxRate != detailedTaxRate)
                    {
                        detaiTaxRate = "No";
                        break;
                    }
                }
            }
            if (detaiTaxRate == "No")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 获取税额和总金额
        /// </summary>
        /// <param name="resaulttaxlv"></param>
        /// <param name="moneyAmount"></param>
        /// <param name="taxAmount"></param>
        /// <param name="bpm"></param>
        private static void getTaxAmountAndTotalAmount(string resaulttaxlv, decimal moneyAmount, decimal taxAmount, decimal totleAmount, string moneytaxhjMax, InvoiiceOcrModel bpm)
        {
            string taxlvs = resaulttaxlv;
            decimal temptaxrate;
            decimal taxamount;
            decimal.TryParse(taxlvs.Replace("%", ""), out temptaxrate);
            if (temptaxrate != 0)
            {
                taxamount = Math.Round((moneyAmount * (temptaxrate / 100)), 2, MidpointRounding.ToEven);
            }
            else {
                taxamount = taxAmount;
            }
            decimal totleMoney = moneyAmount + taxamount;
            bool isIdentical = minMoneyEquelMaxMoney(moneytaxhjMax, totleMoney);
            if (isIdentical)
            {
                string omparisonTaxmoney = getTaxAmounts(taxamount, taxAmount);
                if (string.IsNullOrEmpty(omparisonTaxmoney))
                {
                    bpm.invoiceOcrSonLists[0].taxAmounts = taxamount.ToString("#0.00");
                    bpm.invoiceOcrSonLists[0].totalMoney = totleMoney.ToString("#0.00");
                }
            }

            //else {
            //    if (taxAmount != taxamount) {
            //        decimal money1 =  Math.Round((totleMoney / (1 + (temptaxrate / 100))), 2, MidpointRounding.ToEven);
            //        decimal money2 =  Math.Round((totleAmount / (1 + (temptaxrate / 100))), 2, MidpointRounding.ToEven);
            //        decimal moneyAmoutEnd1 = getmoneyEqual(money1, moneyAmount);
            //        decimal moneyAmoutEnd2 = getmoneyEqual(money2, moneyAmount);
            //        if (moneyAmoutEnd1 !=0) {
            //                bpm.invoiceOcrSonLists[0].taxAmounts = taxamount.ToString("#0.00");
            //                bpm.invoiceOcrSonLists[0].totalMoney = totleMoney.ToString("#0.00");
            //        }
            //    }
            //}
        }
        /// <summary>
        /// 获取税额
        /// </summary>
        /// <param name="resaulttaxlv"></param>
        /// <param name="moneyAmount"></param>
        /// <param name="taxAmount"></param>
        /// <param name="tempTaxMoney"></param>
        /// <returns></returns>
        public static string getTaxAmounts(string resaulttaxlv, decimal moneyAmount, decimal taxAmount, out decimal tempTaxMoney)
        {
            string getTaxAmount = "";
            decimal temptaxrate;
            decimal.TryParse(resaulttaxlv.Replace("%", ""), out temptaxrate);
            tempTaxMoney = Math.Round((moneyAmount * (temptaxrate / 100)), 2, MidpointRounding.AwayFromZero);
            getTaxAmount = getTaxAmounts(tempTaxMoney, taxAmount);
            return getTaxAmount;
        }
        /// <summary>
        /// 给值前面带0 的去掉0
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public static string changNumberDelZero(string money)
        {
            string moneys = "";
            if (!string.IsNullOrEmpty(money))
            {
                decimal moneyZH = 0;
                decimal.TryParse(money, out moneyZH);
                moneys = moneyZH.ToString("#0.00");
            }
            return moneys;
        }
        /// <summary>
        /// 给税率，合计赋值
        /// </summary>
        /// <param name="bpm"></param>
        /// <param name="tempresaulttaxlv"></param>
        /// <param name="moneys"></param>
        public static void setInvoiceTaxRateAndTotleMoneyValue(InvoiiceOcrModel bpm, decimal tempresaulttaxlv, decimal moneys, string taxlvs)
        {
            if (!string.IsNullOrEmpty("taxlvs"))
            {
                bpm.invoiceOcrSonLists[0].taxRate = tempresaulttaxlv + "%";
            }
            else
            {
                bpm.invoiceOcrSonLists[0].taxRate = "";
            }

            bpm.invoiceOcrSonLists[0].totalMoney = moneys.ToString("#0.00");
            bpm.invoiceOcrSonLists[0].totalStateMoney = CompanyOrMoneyStateEnum.MoneyManualCalculation;
        }
        /// <summary>
        /// 比对合计大写是否等于小写
        /// </summary>
        /// <param name="moneytaxhjMax"></param>
        /// <param name="moneys"></param>
        /// <returns></returns>
        public static bool minMoneyEquelMaxMoney(string moneytaxhjMax, decimal moneys)
        {
            string maxm = ToRMB(Math.Abs(moneys));
            string maxmT = ToRMBTwo(Math.Abs(moneys));
            if (!string.IsNullOrEmpty(maxm))
            {
                if (maxm.Contains("整"))
                {
                    maxm = maxm.Replace("整", "");
                }
            }
            if (!string.IsNullOrEmpty(maxmT))
            {
                if (maxmT.Contains("整"))
                {
                    maxmT = maxmT.Replace("整", "");
                }
            }
            if (!string.IsNullOrEmpty(moneytaxhjMax))
            {
                if (moneytaxhjMax.Contains("整"))
                {
                    moneytaxhjMax = moneytaxhjMax.Replace("整", "");
                }
            }

            if (maxm == moneytaxhjMax || maxmT == moneytaxhjMax)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 赋值税额
        /// </summary>
        /// <param name="totleAmount"></param>
        /// <param name="moneyAmount"></param>
        /// <param name="bpm"></param>
        public static void taxMoneyAndTaxRate(decimal totleAmount, decimal moneyAmount, InvoiiceOcrModel bpm)
        {
            decimal taxmoney;
            taxmoney = totleAmount - moneyAmount;
            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxmoney);
            bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
            if (!string.IsNullOrEmpty(Convert.ToString(taxmoney)))
            {
                bpm.invoiceOcrSonLists[0].taxAmounts = taxmoney.ToString("#0.00");
            }
            bpm.invoiceOcrSonLists[0].moneyAmounts = moneyAmount.ToString("#0.00");
        }
        /// <summary>
        /// 给空的金额设置判断
        /// </summary>
        /// <param name="bpm"></param>
        /// <param name="money"></param>
        /// <param name="taxAmounts"></param>
        /// <param name="totalMoney"></param>
        /// <param name="moneytaxhjMax"></param>
        public static void moneysIsNullEvery(InvoiiceOcrModel bpm, string money, string taxAmounts, string totalMoney, string moneytaxhjMax, string taxRate, bool isIdentical, ref decimal moneyAmount, ref decimal taxAmount, ref decimal totleAmount)
        {
            decimal moneyAmountA;
            decimal taxAmountA;
            decimal totleAmountA;
            decimal taxRates;
            decimal.TryParse(money, out moneyAmountA);
            decimal.TryParse(taxAmounts, out taxAmountA);
            decimal.TryParse(totalMoney, out totleAmountA);
            string delBaiFenTaxRate = "";
            if (!string.IsNullOrEmpty(taxRate))
            {
                delBaiFenTaxRate = taxRate.Replace("%", "");
            }
            decimal.TryParse(delBaiFenTaxRate, out taxRates);
            //如果金额为空
            if (string.IsNullOrEmpty(money))
            {
                //税额不为空
                if (!string.IsNullOrEmpty(taxAmounts))
                {
                    //价税合计不为空
                    if (!string.IsNullOrEmpty(totalMoney))
                    {
                        moneyAmountA = Math.Round((totleAmountA - taxAmountA), 2, MidpointRounding.ToEven);
                        bpm.invoiceOcrSonLists[0].moneyAmounts = moneyAmountA.ToString("#0.00");
                        moneyAmount = moneyAmountA;
                        money = Convert.ToString(moneyAmountA);
                        bpm.invoiceOcrSonLists[0].moneyAmountsState = CompanyOrMoneyStateEnum.MoneyManualCalculation;
                    }
                    else
                    {
                        //如果只有税额，如果有税率 税额/税率=金额
                        if (!string.IsNullOrEmpty(taxRate))
                        {
                            if (taxRate != "0%" && taxAmountA != 0 && !isIdentical)
                            {
                                decimal moneyget = Math.Round((taxAmountA / (taxRates / 100)), 2, MidpointRounding.ToEven);
                                decimal totlemoneyget = Math.Round((moneyget + taxAmountA), 2, MidpointRounding.ToEven);
                                bpm.invoiceOcrSonLists[0].moneyAmounts = moneyget.ToString("#0.00");
                                bpm.invoiceOcrSonLists[0].totalMoney = totlemoneyget.ToString("#0.00");
                                moneyAmount = moneyget;
                                totleAmount = totlemoneyget;
                            }
                        }
                    }
                }
                else
                {//税额为空
                    if (!string.IsNullOrEmpty(totalMoney))
                    {
                        if (!string.IsNullOrEmpty(taxRate))
                        {
                            if (totleAmountA!=0&& taxRates!=0) {
                                decimal moneys = Math.Round((totleAmountA / (1 + (taxRates / 100))), 2, MidpointRounding.ToEven);
                                decimal taxmoney = Math.Round((totleAmountA - moneys), 2, MidpointRounding.ToEven);
                                moneyAmount = moneys;
                                taxAmount = taxmoney;
                                bpm.invoiceOcrSonLists[0].moneyAmounts = moneys.ToString("#0.00");
                                bpm.invoiceOcrSonLists[0].taxAmounts = taxmoney.ToString("#0.00");
                            }  
                        }
                    }
                }
            }
            //金额不为空
            else
            {
                //税额不为空
                if (!string.IsNullOrEmpty(taxAmounts))
                {
                    if (string.IsNullOrEmpty(totalMoney))
                    {
                        decimal totlegetmoney = Math.Round((moneyAmountA + totleAmountA), 2, MidpointRounding.ToEven);
                        bpm.invoiceOcrSonLists[0].totalMoney = totlegetmoney.ToString("#0.00");
                        totleAmount = totlegetmoney;
                    }
                }
                //税额为空
                else
                {
                    //合计不为空
                    if (!string.IsNullOrEmpty(totalMoney))
                    {
                        taxAmountA = Math.Round((totleAmountA - moneyAmountA), 2, MidpointRounding.ToEven);
                        if (moneyAmountA > 0 && !isIdentical)
                        {
                            if (taxAmountA < 0)
                            {
                                if (taxRates!=0) {
                                    taxAmountA = Math.Round((moneyAmountA * (taxRates / 100)), 2, MidpointRounding.ToEven);
                                }
                                totleAmountA = Math.Round((moneyAmountA + taxAmountA), 2, MidpointRounding.ToEven);
                                bpm.invoiceOcrSonLists[0].totalMoney = totleAmountA.ToString("#0.00");
                                totleAmount = totleAmountA;
                            }
                        }
                        bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountA.ToString("#0.00");
                        taxAmounts = Convert.ToString(taxAmountA);
                        taxAmount = taxAmountA;
                        bpm.invoiceOcrSonLists[0].taxAmountsState = CompanyOrMoneyStateEnum.MoneyManualCalculation;
                    }
                    //合计为空
                    else
                    {
                        //税率不为空
                        if (!string.IsNullOrEmpty(taxRate) && !isIdentical)
                        {
                            if (taxRates!=0) {
                                taxAmountA = Math.Round((moneyAmountA * (taxRates / 100)), 2, MidpointRounding.ToEven);
                            }              
                            totleAmountA = Math.Round((moneyAmountA + taxAmountA), 2, MidpointRounding.ToEven); ;
                            bpm.invoiceOcrSonLists[0].taxAmounts = taxAmountA.ToString("#0.00");
                            bpm.invoiceOcrSonLists[0].totalMoney = totleAmountA.ToString("#0.00");
                            taxAmount = taxAmountA;
                            totleAmount = totleAmountA;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 得到总金额，税额，计算税额
        /// </summary>
        /// <param name="moneyAmount"></param>
        /// <param name="temptaxrate"></param>
        /// <param name="taxAmount"></param>
        /// <param name="allmoney"></param>
        /// <param name="taxmoney"></param>
        /// <param name="endtaxAmount"></param>
        public static void getmoneys(decimal moneyAmount, decimal temptaxrate, decimal taxAmount, out decimal allmoney, out decimal taxmoney, out decimal endtaxAmount)
        {
            if (moneyAmount != 0 && temptaxrate != 0)
            {
                taxmoney = Math.Round((moneyAmount * temptaxrate) / 100, 2, MidpointRounding.ToEven);
            }
            else {
                taxmoney = taxAmount;
            }
            endtaxAmount = 0;
            string endtaxAmounts = getTaxAmounts(taxmoney, taxAmount);
            if (!string.IsNullOrEmpty(endtaxAmounts))
            {
                decimal.TryParse(endtaxAmounts, out endtaxAmount);
                allmoney = moneyAmount + endtaxAmount;
            }
            else
            {
                allmoney = moneyAmount + taxmoney;
            }
        }

        /// <summary>
        /// 得出税额
        /// </summary>
        /// <param name="taxmoney"></param>
        /// <param name="taxmoneys"></param>
        /// <returns></returns>
        public static string getTaxAmounts(decimal taxmoney, decimal taxmoneys)
        {
            string rightTaxAmount = "";
            decimal taxmoneyZeroOneAdd = taxmoney + 0.01M;
            decimal taxmoneyZeroOneJan = taxmoney - 0.01M;
            decimal taxmoneyZeroTwoAdd = taxmoney + 0.02M;
            decimal taxmoneyZeroTwoJan = taxmoney - 0.02M;
            decimal[] taxmoneylist = { taxmoney, taxmoneyZeroOneAdd, taxmoneyZeroOneJan, taxmoneyZeroTwoAdd, taxmoneyZeroTwoJan };
            foreach (decimal tax in taxmoneylist)
            {
                if (tax == taxmoneys)
                {
                    rightTaxAmount = Convert.ToString(tax);
                    break;
                }
            }
            return rightTaxAmount;
        }
        /// <summary>
        /// 获取金额是否相等
        /// </summary>
        /// <param name="moneyAmoutFax"></param>
        /// <param name="moneyAmount"></param>
        /// <returns></returns>
        public static decimal getmoneyEqual(decimal moneyAmoutFax, decimal moneyAmount)
        {
            decimal moneyTwo = moneyAmoutFax + 0.01M;
            decimal moneyTwo2 = moneyAmoutFax - 0.01M;
            decimal moneyTwo3 = moneyAmoutFax + 0.02M;
            decimal moneyTwo4 = moneyAmoutFax - 0.02M;
            decimal[] moneylist = { moneyAmoutFax, moneyTwo, moneyTwo2, moneyTwo3, moneyTwo4 };
            decimal ednMoney = 0;
            foreach (decimal mt in moneylist)
            {
                if (mt == moneyAmount)
                {
                    ednMoney = mt;
                    break;
                }
            }
            return ednMoney;
        }
        /// <summary>
        /// 获取所有完全真确的税率
        /// </summary>
        /// <param name="moneyAmount"></param>
        /// <param name="taxAmount"></param>
        /// <param name="spareTaxRate"></param>
        /// <param name="productTaxRate"></param>
        /// <returns></returns>
        public static string getAllRightTaxRate(decimal moneyAmount, decimal taxAmount, string spareTaxRate, string productTaxRate)
        {
            string taxlv = "";
            //则可以证明价税合计是正确的，税额是正确的，
            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxAmount);
            //得到税率
            taxlv = getTaxRate(spareTaxRate, productTaxRate, resaulttaxlv);
            return taxlv;
        }
        /// <summary>
        /// 获取销项税率
        /// </summary>
        /// <param name="spareTaxRate"></param>
        /// <param name="productTaxRate"></param>
        /// <param name="taxlv"></param>
        /// <returns></returns>
        public static string getTaxRate(string spareTaxRate, string productTaxRate, string taxlv)
        {
            string taxRate = taxlv;
            if (!string.IsNullOrEmpty(taxRate))
            {
                string taxrae = "";
                foreach (string taxr in taxRateList)
                {
                    if (taxlv == taxr)
                    {
                        taxrae = taxr;
                    }
                }
                if (taxrae != "")
                {
                    taxRate = taxrae;
                }
                else
                {
                    if (!string.IsNullOrEmpty(productTaxRate) || string.IsNullOrEmpty(taxRate))
                    {
                        if (productTaxRate != taxRate)
                        {
                            taxRate = productTaxRate;
                        }

                    }
                    else if (!string.IsNullOrEmpty(spareTaxRate) || string.IsNullOrEmpty(taxRate))
                    {
                        if (spareTaxRate != taxRate)
                        {
                            taxRate = spareTaxRate;
                        }
                    }
                    else
                    {
                        taxRate = taxrae;
                    }
                }

            }
            else if (!string.IsNullOrEmpty(productTaxRate) || string.IsNullOrEmpty(taxRate))
            {
                if (productTaxRate != taxRate)
                {
                    taxRate = productTaxRate;
                }

            }
            else if (!string.IsNullOrEmpty(spareTaxRate) || string.IsNullOrEmpty(taxRate))
            {
                if (spareTaxRate != taxRate)
                {
                    taxRate = spareTaxRate;
                }
            }
            else
            {
                string taxrae = "";
                foreach (string taxr in taxRateList)
                {
                    if (taxlv == taxr)
                    {
                        taxrae = taxr;
                    }
                }
                taxRate = taxrae;
            }
            return taxRate;
        }
        /// <summary>
        /// 设置税率
        /// </summary>
        /// <param name="moneyAmount"></param>
        /// <param name="taxAmount"></param>
        /// <param name="bpm"></param>
        public static void setTaxRate(decimal moneyAmount, decimal taxAmount, InvoiiceOcrModel bpm)
        {
            string resaulttaxlv = calculatingTaxRate(moneyAmount, taxAmount);
            bpm.invoiceOcrSonLists[0].taxRate = resaulttaxlv;
            bpm.invoiceOcrSonLists[0].totalStateMoney = CompanyOrMoneyStateEnum.MoneyCalculationAbsolutelyRight;
        }
        /// <summary>
        /// 将小写金额转化为繁体大写金额 圆变为元
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToRMB(object value)
        {
            try
            {
                string hash = double.Parse(value.ToString()).ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
                string results = Regex.Replace(hash, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[.]|$))))", "${b}${z}");
                hash = Regex.Replace(results, ".", delegate (Match m) { return "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
                if (!string.IsNullOrEmpty(hash))
                {
                    if (hash.Substring(hash.Length - 1, 1) == "圆") { hash += "整"; }
                    if (hash.Contains("整"))
                    {
                        hash = hash.Replace("圆", "元");
                    }
                    return hash;
                }
                else
                {
                    return "零";
                }
            }
            catch (Exception)
            {
                return "零";
            }
        }
        /// <summary>
        /// 将小写金额转化为繁体大写金额 圆不变
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToRMBTwo(object value)
        {
            try
            {
                string hash = double.Parse(value.ToString()).ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
                string results = Regex.Replace(hash, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[.]|$))))", "${b}${z}");
                hash = Regex.Replace(results, ".", delegate (Match m) { return "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
                if (!string.IsNullOrEmpty(hash))
                {
                    if (hash.Substring(hash.Length - 1, 1) == "圆") { hash += "整"; }
                    return hash;
                }
                else
                {
                    return "零";
                }
            }
            catch (Exception)
            {
                return "零";
            }
        }
        /// <summary>
        ///   获取商品名称
        /// </summary>
        public static string getProductName(InvoiiceOcrModel invoiceModel)
        {
            string productName = "";
            if (invoiceModel.invoiceOcrDetailedDtoList.Count() == 1)
            {
                productName = invoiceModel.invoiceOcrDetailedDtoList[0].productName;
            }
            else if ((invoiceModel.invoiceOcrDetailedDtoList.Count() > 1))
            {
                int isIdenticalnum = 0;
                string goodsName = invoiceModel.invoiceOcrDetailedDtoList[0].productName;
                foreach (invoiceOcrDetailedModel iod in invoiceModel.invoiceOcrDetailedDtoList)
                {
                    if (goodsName == iod.productName)
                    {
                        isIdenticalnum++;
                    }
                }
                if (isIdenticalnum == invoiceModel.invoiceOcrDetailedDtoList.Count())
                {
                    productName = goodsName;
                }
            }
            return productName;
        }
        /// <summary>
        /// 手动算税率
        /// </summary>
        /// <param name="money"></param>
        /// <param name="taxmoney"></param>
        /// <returns></returns>
        public static string calculatingTaxRate(decimal moneye, decimal taxmoneye)
        {
            string taxlvend = "";
            if (moneye != 0)
            {
                decimal shulv;
                if (taxmoneye != 0)
                {
                    shulv = Math.Round((taxmoneye / moneye), 2, MidpointRounding.AwayFromZero) * 100;
                }
                else
                {
                    shulv = 0;
                }

                taxlvend = InvoiceOcrTool.setTaxLv(shulv);
            }

            return taxlvend;
        }
        /// <summary>
        /// 通过商品名称获取税率
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        public static string getProductTaxRate(string productName)
        {
            string productTaxRate = "";
            List<BatchInfo> productNameList = new List<BatchInfo>();
            var productNamedataList = from complist in HongFengCompanyOrVendorsTool.ProductNameAll where complist.productName == productName select complist;
            productNameList = productNamedataList.ToList();
            if (productNameList.Count > 0)
            {
                productTaxRate = productNameList[0].defaultTaxRate;
            }
            return productTaxRate;
        }
        /// <summary>
        /// 通过是否销项获取税率
        /// </summary>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public static string getSpareTaxRate(string companyName)
        {
            string spareTaxRate = "";
            List<BatchInfo> companyList = new List<BatchInfo>();
            var companyListdataList = from complist in HongFengCompanyOrVendorsTool.companyAll where complist.companyName == companyName select complist;
            companyList = companyListdataList.ToList();
            if (companyList.Count > 0)
            {
                spareTaxRate = companyList[0].defaultTaxRate;
            }
            return spareTaxRate;
        }
        /// <summary>
        /// 根据税率是否等于免税，不征税等字样半段是否为0%
        /// </summary>
        /// <param name="taxRate"></param>
        /// <returns></returns>
        public static bool getTaxRate(string taxRate)
        {
            if (!string.IsNullOrEmpty(taxRate))
            {
                if (taxRate.Contains("免税") || taxRate.Contains("免") || taxRate.Contains("税"))
                {
                    return true;
                }
                else if (taxRate.Contains("不征税") || taxRate.Contains("不") || taxRate.Contains("征"))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 通过计算得出的合计与大写比对
        /// </summary>
        /// <param name="totleje"></param>
        /// <param name="moneytaxhjMax"></param>
        /// <returns></returns>
        public static string getTaxAmount(decimal totleje, string moneytaxhjMax)
        {
            string taxAmount = "";
            Dictionary<string, decimal> CapitalandCaseAmounts = new Dictionary<string, decimal>();
            Dictionary<string, decimal> CapitalandCaseAmountsTwo = new Dictionary<string, decimal>();
            getBigSmallMoney(totleje, out CapitalandCaseAmounts, out CapitalandCaseAmountsTwo);
            foreach (KeyValuePair<string, decimal> keyValue in CapitalandCaseAmounts)
            {
                if (keyValue.Key == moneytaxhjMax)
                {
                    taxAmount = Convert.ToString(keyValue.Value);
                    break;
                }
            }
            if (taxAmount == "")
            {
                foreach (KeyValuePair<string, decimal> keyValueTwo in CapitalandCaseAmountsTwo)
                {
                    if (keyValueTwo.Key == moneytaxhjMax)
                    {
                        taxAmount = Convert.ToString(keyValueTwo.Value);
                        break;
                    }
                }
            }
            return taxAmount;
        }
        /// <summary>
        /// 将大写及其对应小写放入到字典当中
        /// </summary>
        /// <param name="totleje"></param>
        /// <param name="CapitalandCaseAmounts"></param>
        /// <param name="CapitalandCaseAmountsTwo"></param>
        public static void getBigSmallMoney(decimal totleje, out Dictionary<string, decimal> CapitalandCaseAmounts, out Dictionary<string, decimal> CapitalandCaseAmountsTwo)
        {
            CapitalandCaseAmounts = new Dictionary<string, decimal>();
            CapitalandCaseAmountsTwo = new Dictionary<string, decimal>();
            string bigTotleMoneys = ToRMB(Math.Abs(totleje));
            string bigTotleMoneys2 = ToRMB(Math.Abs(totleje + 0.01M));
            string bigTotleMoneys3 = ToRMB(Math.Abs(totleje - 0.01M));
            string bigTotleMoneys4 = ToRMB(Math.Abs(totleje + 0.02M));
            string bigTotleMoneys5 = ToRMB(Math.Abs(totleje - 0.02M));
            string[] bigTotleMoneyslist = { bigTotleMoneys, bigTotleMoneys2, bigTotleMoneys3, bigTotleMoneys4, bigTotleMoneys5 };
            string bigTotleMoneysB = ToRMBTwo(Math.Abs(totleje));
            string bigTotleMoneysC = ToRMBTwo(Math.Abs(totleje + 0.01M));
            string bigTotleMoneysD = ToRMBTwo(Math.Abs(totleje - 0.01M));
            string bigTotleMoneysE = ToRMBTwo(Math.Abs(totleje + 0.02M));
            string bigTotleMoneysF = ToRMBTwo(Math.Abs(totleje - 0.02M));
            string[] bigTotleMoneyslistB = { bigTotleMoneysB, bigTotleMoneysC, bigTotleMoneysD, bigTotleMoneysE, bigTotleMoneysF };
            foreach (string ss in bigTotleMoneyslistB)
            {

            }
            CapitalandCaseAmounts.Add(bigTotleMoneys, totleje);
            if (!CapitalandCaseAmounts.ContainsKey(bigTotleMoneys2))
            {
                CapitalandCaseAmounts.Add(bigTotleMoneys2, totleje + 0.01M);
            }
            if (!CapitalandCaseAmounts.ContainsKey(bigTotleMoneys3))
            {
                CapitalandCaseAmounts.Add(bigTotleMoneys3, totleje - 0.01M);
            }
            if (!CapitalandCaseAmounts.ContainsKey(bigTotleMoneys4))
            {
                CapitalandCaseAmounts.Add(bigTotleMoneys4, totleje + 0.02M);
            }
            if (!CapitalandCaseAmounts.ContainsKey(bigTotleMoneys5))
            {
                CapitalandCaseAmounts.Add(bigTotleMoneys5, totleje - 0.02M);
            }

            CapitalandCaseAmountsTwo.Add(bigTotleMoneysB, totleje);
            if (!CapitalandCaseAmountsTwo.ContainsKey(bigTotleMoneysC))
            {
                CapitalandCaseAmountsTwo.Add(bigTotleMoneysC, totleje + 0.01M);
            }
            if (!CapitalandCaseAmountsTwo.ContainsKey(bigTotleMoneysD))
            {
                CapitalandCaseAmountsTwo.Add(bigTotleMoneysD, totleje - 0.01M);
            }
            if (!CapitalandCaseAmountsTwo.ContainsKey(bigTotleMoneysE))
            {
                CapitalandCaseAmountsTwo.Add(bigTotleMoneysE, totleje + 0.02M);
            }
            if (!CapitalandCaseAmountsTwo.ContainsKey(bigTotleMoneysF))
            {
                CapitalandCaseAmountsTwo.Add(bigTotleMoneysF, totleje - 0.02M);
            }
        }
    }
}
